package com.itheima.controller;

import com.itheima.pojo.BlogInfo;
import com.itheima.service.ESManagerService;
import com.itheima.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/search")
@Api(tags = "SQL搜索相关接口")
@CrossOrigin
public class ESManagerController {
    @Autowired
    private ESManagerService esManagerService;

    @PostMapping("/initElasticSearchIndex")
    @ApiOperation("初始化ElasticSearch搜素引擎")
    public Result initElasticSearchIndex(){
        esManagerService.initElasticSearchIndex();
        return new Result("success","创建索引库成功");
    }

    @PostMapping("/importAll")
    public Result importAll(){
        esManagerService.importAll();
        return new Result("success","导入全部数据成功");
    }

    @PostMapping("/importByUid")
    public Result importByUid(@RequestParam("uid") String uid){
        esManagerService.importByUid(uid);
        return new Result("success","根据uid导入成功");
    }

    @PostMapping("/deleteByUid")
    public Result deleteByUid(@RequestParam("uid") String uid){
        esManagerService.deleteByUid(uid);
        return new Result("success","根据uid删除成功");
    }

    @PostMapping("/deleteByUids")
    public Result deleteByUids(@RequestParam("uids") String uids){
        esManagerService.deleteByUids(uids);
        return new Result("success","根据uids删除成功");
    }

    @GetMapping("/getByKeywords")
    public Result getByKeyWords(String keywords,Integer currentPage,Integer pageSize){
        List<BlogInfo> blogInfoList = esManagerService.getByKeywords(keywords, currentPage, pageSize);
        return new Result("success",blogInfoList);
    }

    @GetMapping("/sqlSearchBlog")
    @ApiOperation("搜索blog")
    public Result sqlSearchBlog(String keyWords, Integer currentPage , Integer pageSize){
        Page<BlogInfo> blogInfos = esManagerService.sqlSearchBlog(keyWords, currentPage, pageSize);
        Map<String,Object> map=new HashMap<>();
        map.put("total",blogInfos.getTotalElements());
        map.put("toatalPage",blogInfos.getTotalPages());
        map.put("pageSize",pageSize);
        map.put("currentPage",currentPage);
        map.put("blogList",blogInfos.getContent());
        return new Result("success",map);
    }

    @GetMapping("/searchBlogByAuthor")
    @ApiOperation("根据作者获取相关的博客")
    public Result searchBlogByAuthor(String author, Integer currentPage, Integer pageSize){
        List<BlogInfo> blogInfos = esManagerService.searchBlogByAuthor(author, currentPage, pageSize);
        return new Result("success",blogInfos);
    }

    @GetMapping("/searchBlogBySort")
    @ApiOperation("根据分类获取相关的博客")
    public Result searchBlogBySort(String blogSortUid,Integer currentPage ,Integer pageSize){
        List<BlogInfo> blogInfos = esManagerService.searchBlogByAuthor(blogSortUid, currentPage, pageSize);
        return new Result("success",blogInfos);
    }

    @GetMapping("/searchBlogByTag")
    @ApiOperation("根据标签获取相关的博客")
    public Result searchBlogByTag(String tagUid, Integer currentPage, Integer pageSize){
        List<BlogInfo> blogInfos = esManagerService.searchBlogByAuthor(tagUid, currentPage, pageSize);
        return new Result("success",blogInfos);
    }

}
